import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMailSSL {
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {

	Properties props = new Properties();
	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.smtp.socketFactory.port", "465");
	props.put("mail.smtp.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.port", "465");

	Session session = Session.getDefaultInstance(props,
		new javax.mail.Authenticator() {
		    protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(
				"java@gmail.com", "");
		    }
		});

	try {
	    Map<String, String> senderslist = getSendersList();
	    Iterator it = senderslist.entrySet().iterator();
	    while (it.hasNext()) {

		Map.Entry entry = (Map.Entry) it.next();
		String mailto = (String) entry.getKey();
		String val = (String) entry.getValue();

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress("java@gmail.com"));
		message.setRecipients(Message.RecipientType.TO,
			InternetAddress.parse(mailto));
		message.setSubject(getSubject());
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText("This is message body");
		//message.setText("Hi" + val + "\n\n" + getText());
		
		// Create a multipar message
	         Multipart multipart = new MimeMultipart();
	         // Set text message part
	         multipart.addBodyPart(messageBodyPart);
	         
	      // Part two is attachment
	         messageBodyPart = new MimeBodyPart();
	         String filename = "SendMailSSL.java";
	         DataSource source = new FileDataSource("C:\\Java training\\ws\\sendemail\\src\\SendMailSSL.java");
	         messageBodyPart.setDataHandler(new DataHandler(source));
	         messageBodyPart.setFileName(filename);
	         multipart.addBodyPart(messageBodyPart);

	         // Send the complete message parts
	         message.setContent(multipart );
	         
	         
		Transport.send(message);

		System.out.println("sent to " + val + "- -" + mailto);
	    }

	} catch (MessagingException e) {
	    throw new RuntimeException(e);
	}
    }

    private static Map<String, String> getSendersList() {

	Map<String, String> map = new HashMap<String, String>();

	map.put("@gmail.com", "venki");

	 
	return map;

    }

    private static String getSubject() {
	return "Availble for Java/J2EE position";
    }

    private static String getText() {
	String textTosent = "\n\n No spam to my email, please!";

	return textTosent;
    }

}